﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;


namespace AzureBlob
{
    public class AccountInfo
    {
        CloudBlobClient client = null;
        

        private AccountInfo()
        {
        }

        public static AccountInfo Development
        {
            get
            {
                AccountInfo accountInfo = new AccountInfo();
                accountInfo.AccountName = "(Development)";
                accountInfo.AccountKey = string.Empty;
                accountInfo.Connected = true;
                accountInfo.CDN = string.Empty;
                return accountInfo;
            }
        }

        public override bool Equals(object obj)
        {
            return obj is AccountInfo && (obj as AccountInfo).AccountName.Equals(AccountName);
        }

        public override int GetHashCode()
        {
            return AccountName.GetHashCode();
        }

        public AccountInfo(string accountName, string accountKey, string cdn, bool connect)
        {
            this.AccountName = accountName;
            this.AccountKey = accountKey;
            this.CDN = cdn;
            this.Connected = connect;
        }

        public string AccountName { get; set; }
        public string AccountKey { get; set; }
        public bool Connected { get; set; }
        public string CDN { get; set; }

        internal CloudBlobClient GetBlobClient()
        {
            CloudStorageAccount Account = AccountInfo.Development.Equals(this) 
                ? CloudStorageAccount.DevelopmentStorageAccount
                : new CloudStorageAccount(new StorageCredentialsAccountAndKey(AccountName, AccountKey), false);
            if (client == null)
                client = Account.CreateCloudBlobClient();
            client.Timeout = new TimeSpan(1, 0, 0);
            client.RetryPolicy = RetryPolicies.Retry(20, TimeSpan.Zero);
            client.WriteBlockSizeInBytes = 4 * 1024 * 1024;
            return client;
        }
    }
}
